#include"Process.h"

void handle_owl(const char* input,const char* output_inst_con,const char* output_con_sub){
	 try {
    XMLPlatformUtils::Initialize();
  }
  catch (const XMLException& toCatch) {
    // Do your failure processing here
    exit(1);
  }


  SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
  parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
  parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);   // optional

  TaxonomyHandler* defaultHandler = new TaxonomyHandler();
  parser->setContentHandler(defaultHandler);
  parser->setErrorHandler(defaultHandler);

   try {
            parser->parse(input);
        }
   catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);
            exit(-1);
   }
   catch (const SAXParseException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);
            exit(-1);
   }
   catch (...) {
            cout << "Unexpected Exception \n" ;
            exit(-1);
   }

   defaultHandler->SaveAsMyFormat(output_inst_con,output_con_sub);

   delete parser;
   delete defaultHandler;


   XMLPlatformUtils::Terminate();

}

void handle_wsdl(const char* input,const char* output){
	 try {
    XMLPlatformUtils::Initialize();
  }
  catch (const XMLException& toCatch) {
    // Do your failure processing here
    exit(1);
  }


  SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
  parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
  parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);   // optional



  WsdlHandler* defaultHandler = new WsdlHandler();
  parser->setContentHandler(defaultHandler);
  parser->setErrorHandler(defaultHandler);

   try {
            parser->parse(input);
        }
   catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);
            exit(-1);
   }
   catch (const SAXParseException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);
            exit(-1);
   }
   catch (...) {
            cout << "Unexpected Exception \n" ;
            exit(-1);
   }

   defaultHandler->SaveAsMyFormat(output);

   delete parser;
   delete defaultHandler;


   XMLPlatformUtils::Terminate();
}


void combine_wsdl_concept(const char* wsdl,const char* inst_con,const char* output){
	FILE* fp;
	if((fp=fopen(wsdl,"r")) == NULL){
		printf("Error:file cannot open!\n");
		getchar();
		exit(-1);
	}
	char buffer[200];
	vector<Service> services;
	if(fscanf(fp,"%s\n",buffer)){
		string line(buffer);
		const regex pattern("(\\d*):\\[\\]\\[(.*)\\]\\[(.*)\\]");
		match_results<string::const_iterator> results;
		string s = results[1].str();
		if(regex_match(line,results,pattern))
			cout<<results[1]<<endl;
	}
	fclose(fp);
	getchar();
	//Service service;
}